package com.aptemo.webetek.client.widgets.games;

import java.util.ArrayList;
import java.util.Date;

import com.aptemo.webetek.client.enums.EnumEventStatus;
import com.aptemo.webetek.client.events.HasPickClickHandlers;
import com.aptemo.webetek.client.widgets.DateTimeWidget;
import com.aptemo.webetek.shared.EventBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class GameRow
		extends Composite implements GameRowPresenter.Display
{

	private static GameRowUiBinder	uiBinder	= GWT.create(GameRowUiBinder.class);

	interface GameRowUiBinder extends UiBinder<Widget, GameRow>
	{
	}

	interface MyStyle extends CssResource
	{
		String active();

		String inactive();

	}

	@UiFactory
	DateTimeWidget createDateTimeWidget()
	{
		return new DateTimeWidget(DateTimeFormat.getMediumDateTimeFormat());
	}

	@UiField
	MyStyle			style;

	@UiField
	Label			gameTitle;

	@UiField
	Label			gameDescription;

	@UiField
	DateTimeWidget	gameDate;

	// @UiField
	// Anchor myPicks;

	@UiField
	BetPicker		betPicker;

	@UiField
	Label			betResultDescription;

	@UiField
	Label			statusField;

	// private PopupPanel myPicksPopup = new PopupPanel(true);

	public GameRow(EventBean bet)
	{
		initWidget(uiBinder.createAndBindUi(this));

		gameTitle.setText(bet.getTitle());
		gameDescription.setText(bet.getDescription());

		betResultDescription.setText(bet.getResultDescription());

		Element el = getElement();
		el.addClassName(bet.getStatus() == EnumEventStatus.ACTIVE ? style.active() : style.inactive());
		el.removeClassName(bet.getStatus() == EnumEventStatus.ACTIVE ? style.inactive() : style.active());
	}

	@Override
	public HasValue<Date> getDateField()
	{
		return gameDate;
	}

	@Override
	public HasText getNameField()
	{
		return gameTitle;
	}

	@Override
	public HasText getDescField()
	{
		return gameDescription;
	}

	@Override
	public void setBetPicksData(EventBean data, ClickHandler handler)
	{
		betPicker.setData(data);
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public String getClickedLink(ClickEvent event)
	{
		Anchor a = (Anchor) event.getSource();

		return a.getText();
	}

	@Override
	public void setMyPicks(ArrayList<String> list)
	{
		for (final String item : list)
		{
			betPicker.selectMine(item);
		}
	}

	// @UiHandler("myPicks")
	// void myPicksClinkHandler(ClickEvent event)
	// {
	// Widget source = (Widget) event.getSource();
	// int left = source.getAbsoluteLeft() + 10;
	// int top = source.getAbsoluteTop() + 10;
	//
	// myPicksPopup.setPopupPosition(left, top);
	// myPicksPopup.show();
	//
	// event.stopPropagation();
	// }

	@Override
	public HasPickClickHandlers getPickSelector()
	{
		return betPicker;
	}

	// @Override
	// public HasClickHandlers getMyBetsLink()
	// {
	// return myPicks;
	// }

}
